package com.jmlee.controller;

import com.jmlee.component.HostHolder;
import com.jmlee.pojo.Admin;
import com.jmlee.pojo.ThisMonthWeekDate;
import com.jmlee.service.AStatisticsService;
import com.jmlee.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;


/**
 * 统计数据
 *
 * @author xl W
 * @date 2021/3/16 10:35
 */
@Controller
public class StatisticsController {

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private AStatisticsService aStatisticsService;

    //====================================用户统计======================================================
    //跳转用户统计
//    @RequestMapping("turnUserTotal")
//    public String turnUserTotal() {
//        return "userTotal";
//    }
    @RequestMapping("turnUserTotal")
    public String turnUserTotal() {
        return "userTotal";
    }

    //统计每周的总数


    //用户统计
    @RequestMapping("userWeek")
    @ResponseBody
    public String userWeek(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("每周来了");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        String[] dates = new String[7];
        for (int i = 0; i < 7; i++) {
            dates[i] = dateFormat.format(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }

        List<Integer> userWeek = aStatisticsService.userWeek(dates);
        String json = JSONUtil.objectToJson(userWeek);


        return json;
//        return null;
    }

    //统计一个月的用户总数
    @RequestMapping("userMonth")
    @ResponseBody
    public String userMonth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Integer> userWeek = aStatisticsService.userMonth();

        String json = JSONUtil.objectToJson(userWeek);


        return json;
    }

    //统计半年的用户总数
    @RequestMapping("userYears")
    @ResponseBody
    public String userYears(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<String> monthName = ThisMonthWeekDate.getMonthName();

        List<Integer> userBanYear = aStatisticsService.userYears();

        String json = JSONUtil.objectToJson(monthName);
        String json1 = JSONUtil.objectToJson(userBanYear);


        return json + "&&" + json1;
    }

    //====================================订单统计======================================================

    /**
     * 跳转订单统计页面
     *
     * @author xl W
     * @date 2021/3/14 18:05
     */
    @RequestMapping("turnOrdersTotal")
    public String turnOrdersTotal() {
        return "ordersTotal";
    }

    //统计每周的总数
    @RequestMapping("ordersWeek")
    @ResponseBody
    public String ordersWeek(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("每周来了");
        Admin admin = hostHolder.getAdmin();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        String[] dates = new String[7];
        for (int i = 0; i < 7; i++) {
            dates[i] = dateFormat.format(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }

        List<Integer> ordersWeek = aStatisticsService.ordersWeek(dates, admin.getId());
        String json = JSONUtil.objectToJson(ordersWeek);


        return json;
//        return null;
    }

    //统计一个月的用户总数
    @RequestMapping("ordersMonth")
    @ResponseBody
    public String ordersMonth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("本月");
        Admin admin = hostHolder.getAdmin();

        List<Integer> ordersMonth = aStatisticsService.ordersMonth(admin.getId());

        String json = JSONUtil.objectToJson(ordersMonth);


        return json;
    }

    //统计半年的用户总数
    @RequestMapping("ordersYears")
    @ResponseBody
    public String ordersYears(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        System.out.println("半年进来了");
        Admin admin = hostHolder.getAdmin();


        List<String> monthName = ThisMonthWeekDate.getMonthName();

        List<Integer> ordersYears = aStatisticsService.ordersYears(admin.getId());

        String json = JSONUtil.objectToJson(monthName);
        String json1 = JSONUtil.objectToJson(ordersYears);


        return json + "&&" + json1;
    }

    //======================================商品销量统计==============
    @RequestMapping("turnSalesTotal")
    public String turnSalesTotal() {
        return "salesTotal";
    }

    //统计每周的销量总数
    @RequestMapping("salesWeek")
    @ResponseBody
    public String salesWeek(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Admin admin = hostHolder.getAdmin();
//        System.err.println(admin.getId());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        String[] dates = new String[7];
        for (int i = 0; i < 7; i++) {
            dates[i] = dateFormat.format(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }

        List<Integer> ordersWeek = aStatisticsService.salesWeek(dates, admin.getId());
        String json = JSONUtil.objectToJson(ordersWeek);


        return json;
//        return null;
    }

    //统计一个月的销量总数
    @RequestMapping("salesMonth")
    @ResponseBody
    public String salesMonth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Admin admin = hostHolder.getAdmin();
        List<Integer> ordersMonth = aStatisticsService.salesMonth(admin.getId());
        String json = JSONUtil.objectToJson(ordersMonth);
        return json;
    }

    //统计半年的销量总数
    @RequestMapping("salesYears")
    @ResponseBody
    public String salesYears(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Admin admin = hostHolder.getAdmin();


        List<String> monthName = ThisMonthWeekDate.getMonthName();

        List<Integer> ordersYears = aStatisticsService.salesYears(admin.getId());

        String json = JSONUtil.objectToJson(monthName);
        String json1 = JSONUtil.objectToJson(ordersYears);


        return json + "&&" + json1;

    }


}
